<?php


namespace app\task;


use app\api\logic\OrderLogic;
use app\model\order\Order;
use Carbon\Carbon;
use support\contract\ITask;
use support\utils\Time;
use Workerman\Crontab\Crontab;

class OrderTask implements ITask
{
    public function start()
    {
        // 每5分钟执行一次 关闭30分钟前的待支付订单
        new Crontab('0 */5 * * * *', function () {
            $logic = new OrderLogic();

            $orderList = Order::where([
                ['create_time', "<", Carbon::now()->subMinutes(30)->format(Time::DATE_TIME)],
                ['status', '=', Order::STATUS_WAIT_PAID],
                ['pay_status', '=', Order::PAY_STATUS_WAIT],
            ])
                ->column('order_no');
            foreach ($orderList as $order_no) {
                $logic->closeOrderOperation($order_no, [
                    'status' => Order::STATUS_CLOSE,
                    'close_time' => Time::now(),
                ]);
            }

        });
    }
}